/**
 * @author 徐楠
 * @date 2021/12/9 23:35
 * @version 1.0
 */

package com.wky.curriculumDesign.views;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class CurriculumDesignViews {

    public final int processListMAXLength = 10;
    public PCB[] inputProcessList = new PCB[processListMAXLength];
    public PCB[] readyProcessList = new PCB[processListMAXLength];
    public PCB runProcess = new PCB();
    public PCB[] endProcessList = new PCB[processListMAXLength];
    public PCB[] blockingProcessList = new PCB[processListMAXLength];
    public PCB[] waitingRunningProcessList = new PCB[processListMAXLength];


    //进程状态
    public final String RUNNING = "RUNNING";
    public final String READY = "READY";
    public final String END = "END";
    public final String BLOCK = "BLOCK";

    //定义一大小为4的物理块数组（4块物理块）
    //PhysicalBlock[] physicalBlocks = new PhysicalBlock[4];
    //内存中空闲块数组
    int[] memoryIntArray = selectMemoryFreeArea();
    //指令地址
    int[] ordersAddress = new int[300];
    //程序计数器，用来记录当前指令的序号
    int pc;
    //缺页计数器，用来记录缺页的次数
    int n;
    //指令总长度
    int orderLength = 0;


    //存地址值的数组开始索引
    int ordersAddressFromIndex = 0;
    //存地址值的数组结束索引
    int ordersAddressTOIndex = 0;

    //存地址值的数组开始索引
    int physicalBlockIDArrayFromIndex = 0;
    //存地址值的数组结束索引
    int physicalBlockIDArrayTOIndex = 0;

    //数组长度
    int processAddressLength0;
    int processAddressLength1;
    int processAddressLength2;
    int processAddressLength3;
    int processAddressLength4;

    //各进程名
    String processName0;
    String processName1;
    String processName2;
    String processName3;
    String processName4;

    private JPanel mainJPanel;
    private JList list1;
    private JList list2;
    private JList list3;
    private JList list4;
    private JList list5;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JTextField textField5;
    private JTextField textField6;
    private JTextField textField7;
    private JTextField textField8;
    private JTextField textField9;
    private JTextField textField10;
    private JButton inputButton;
    private JButton inputButton1;
    private JList list6;
    private JList list7;
    private JList list8;
    private JList list9;
    private JList list10;
    private JList list11;
    private JTextField textField11;
    private JTextField textField12;
    private JTextField textField13;
    private JTextField textField14;
    private JTextField textField15;
    private JButton Button1;
    private JTextField textField16;
    private JList list12;
    private JList list13;
    private JList list14;
    private JList list15;
    private JList list16;
    private JList list17;
    private JTextField textField17;
    private JTextField textField18;


    public CurriculumDesignViews() {
        processName0 = "tyy";
        int priority1 = 9;
        int CPURunningTime1 = 13;
        String state1 = READY;
        int blockingTime1 = 0;
        int orders1 = 13;

        processName1 = "xxw";
        int priority2 = 38;
        int CPURunningTime2 = 8;
        String state2 = READY;
        int blockingTime2 = 0;
        int orders2 = 8;

        processName2 = "yyy";
        int priority3 = 30;
        int CPURunningTime3 = 14;
        String state3 = READY;
        int blockingTime3 = 0;
        int orders3 = 14;

        processName3 = "lol";
        int priority4 = 28;
        int CPURunningTime4 = 11;
        String state4 = READY;
        int blockingTime4 = 0;
        int orders4 = 11;

        processName4 = "game";
        int priority5 = 0;
        int CPURunningTime5 = 15;
        String state5 = READY;
        int blockingTime5 = 0;
        int orders5 = 15;

        //初始化数组
        initialization();


        //给进程赋值
        inputProcessList[0].processName = processName0;
        inputProcessList[0].priority = priority1;
        inputProcessList[0].CPURunningTime = CPURunningTime1;
        inputProcessList[0].state = state1;
        inputProcessList[0].blockingTime = blockingTime1;
        inputProcessList[0].orders = orders1;
        inputProcessList[0].ordersLOCK = orders1;

        inputProcessList[1].processName = processName1;
        inputProcessList[1].priority = priority2;
        inputProcessList[1].CPURunningTime = CPURunningTime2;
        inputProcessList[1].state = state2;
        inputProcessList[1].blockingTime = blockingTime2;
        inputProcessList[1].orders = orders2;
        inputProcessList[1].ordersLOCK = orders2;

        inputProcessList[2].processName = processName2;
        inputProcessList[2].priority = priority3;
        inputProcessList[2].CPURunningTime = CPURunningTime3;
        inputProcessList[2].state = state3;
        inputProcessList[2].blockingTime = blockingTime3;
        inputProcessList[2].orders = orders3;
        inputProcessList[2].ordersLOCK = orders3;

        inputProcessList[3].processName = processName3;
        inputProcessList[3].priority = priority4;
        inputProcessList[3].CPURunningTime = CPURunningTime4;
        inputProcessList[3].state = state4;
        inputProcessList[3].blockingTime = blockingTime4;
        inputProcessList[3].orders = orders4;
        inputProcessList[3].ordersLOCK = orders4;

        inputProcessList[4].processName = processName4;
        inputProcessList[4].priority = priority5;
        inputProcessList[4].CPURunningTime = CPURunningTime5;
        inputProcessList[4].state = state5;
        inputProcessList[4].blockingTime = blockingTime5;
        inputProcessList[4].orders = orders5;
        inputProcessList[4].ordersLOCK = orders5;

        //初始化进程物理块
        initializationPhysicalBlock();

        //长度赋值
        for (int i = 0; i < 5; i++) {
            orderLength = orderLength + inputProcessList[i].orders;
        }

        //生成随机地址数组
        createRandomAddress();

        //每个进程地址数组赋值
        for (int i = 0; i < inputProcessList.length; i++) {
            ordersAddressTOIndex = ordersAddressTOIndex + inputProcessList[i].orders;
            inputProcessList[i].orderSet = Arrays.copyOfRange(ordersAddress, ordersAddressFromIndex, ordersAddressTOIndex);
            ordersAddressFromIndex = ordersAddressTOIndex;
        }

        //给数组从大到小排序
        readyProcessListSort(inputProcessList);

        //把inputProcessList赋值给readyProcessList
        for (int i = 0; i < processListMAXLength; i++) {
            if (i < inputProcessList.length) {
                readyProcessList[i] = inputProcessList[i];
            } else {
                readyProcessList[i] = new PCB();
            }
        }

        //把readyProcessList中优先级最高且状态为ready的赋值给runProcess
        for (int i = 0; i < readyProcessList.length; i++) {
            if (readyProcessList[i].state.equals(READY)) {
                runProcess = readyProcessList[i];
                break;
            }
        }

        //将整在运行的程序的页表显示出来
        String[] runningProcessAddressArrayString = new String[runProcess.ordersLOCK];
        intArrayTOStringArray(runProcess.orderSet, runningProcessAddressArrayString);
        list13.setListData(runningProcessAddressArrayString);

        String[] runningProcessPageHome = new String[runProcess.ordersLOCK];
        InitializeProcessPageHome(runningProcessPageHome);

        //把数据展示到等待进程面板和运行进程面板
        int runProcessIndex = -1;
        for (int i = 0; i < readyProcessList.length; i++) {
            if (readyProcessList[i] == runProcess && readyProcessList[i] != null) {
                runProcessIndex = i;
                break;
            }
        }
        waitingRunningProcessList = removeArrayEmptyTextBackNewArray(readyProcessList, READY, runProcessIndex);
        //将waitingRunningProcessList显示在等待进程面板上
        showWaitingProcessBox();

        //将运行中阻塞的程序存入blockingProcessList
        blockingProcessList = removeArrayEmptyTextBackNewArray(readyProcessList, BLOCK, -1);
        //将阻塞的进程显示在面板
        showBlockingProcessBox();

        //显示运行框数据
        textField11.setText(runProcess.processName);
        textField12.setText(String.valueOf(runProcess.priority));
        textField13.setText(String.valueOf(runProcess.CPURunningTime));
        textField14.setText(RUNNING);
        textField15.setText(String.valueOf(runProcess.orders));

        /*
        //内存选取物理块情况
        selectPhysicalBlockFromMemory();*/

        textField4.setText(READY);
        textField9.setText(READY);


        Button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                //运行进程
                if (runProcess != null && runProcess.CPURunningTime > 0 && runProcess.state != null && runProcess.state.equals(READY)) {
                    runProcess.priority = runProcess.priority - 3;
                    runProcess.CPURunningTime = runProcess.CPURunningTime - 1;
                    runProcess.orders = runProcess.orders - 1;

                    for (int i = 0; i < readyProcessList.length; i++) {
                        if (runProcess.processName.equals(readyProcessList[i].processName)) {
                            LRU(runProcess.orderSet[runProcess.processAddressArrayIndex++]);
                        }
                    }

                    //输入与输出变成BLOCK，阻塞时间为2个时间片
                    //runProcess运行后菜显示指令执行的内容，而runProcess显示的已经变成下一个准备执行的东西
                    String orderContent = ordersExecution();
                    if (orderContent.equals("输入") || orderContent.equals("输出")) {
                        runProcess.state = BLOCK;
                        runProcess.blockingTime = 2;
                    }
                }

                //等待进程优先级+1，如果是BLOCK进程：blocking时间 - 1
                for (int i = 0; i < readyProcessList.length; i++) {

                    if (runProcess == readyProcessList[i]) {
                        continue;
                    }
                    if (readyProcessList[i] == null) {
                        continue;
                    }
                    if (readyProcessList[i].state == null) {
                        continue;
                    }
                    if (readyProcessList[i].state.equals(READY)) {
                        readyProcessList[i].priority = readyProcessList[i].priority + 1;
                    } else if (readyProcessList[i].state.equals(BLOCK)) {
                        readyProcessList[i].priority = readyProcessList[i].priority + 1;
                        readyProcessList[i].blockingTime = readyProcessList[i].blockingTime - 1;
                    } else if (readyProcessList[i].state.equals(END)) {
                        continue;
                    }
                }

                //处理进程blocking时间、CPU运行时间、指令
                for (int i = 0; i < readyProcessList.length; i++) {

                    if (readyProcessList[i].blockingTime <= 0) {
                        readyProcessList[i].blockingTime = 0;
                    }
                    if (readyProcessList[i].CPURunningTime <= 0) {
                        readyProcessList[i].CPURunningTime = 0;
                    }
                    if (readyProcessList[i].orders <= 0) {
                        readyProcessList[i].orders = 0;
                    }
                }

                //处理进程状态
                for (int i = 0; i < readyProcessList.length; i++) {

                    if (readyProcessList[i].blockingTime == 0) {
                        readyProcessList[i].state = READY;
                    }
                    if (readyProcessList[i].CPURunningTime == 0) {
                        readyProcessList[i].state = END;
                    }
                }

                //设置页表中页号对应的物理块号
                if (runProcess != null && !runProcess.processName.equals("NONE")) {
                    String[] runningProcessAddressArrayString = new String[runProcess.ordersLOCK];
                    System.out.println(runProcess.ordersLOCK);
                    intArrayTOStringArray(runProcess.orderSet, runningProcessAddressArrayString);
                    list13.setListData(runningProcessAddressArrayString);
                    String[] runningProcessPageHome = new String[runProcess.ordersLOCK];
                    changeProcessPageHome(runProcess.orderSet, runningProcessPageHome);
                    list14.setListData(runningProcessPageHome);

                }

                //给数组从大到小排序
                readyProcessListSort(readyProcessList);

                runProcess = null;

                for (int i = 0; i < readyProcessList.length; i++) {
                    if (readyProcessList[i].state.equals(READY)) {
                        runProcess = readyProcessList[i];
                        break;
                    }
                }

                //找到runProcess在readyProcessList中的位置
                int runProcessIndex = -1;
                for (int i = 0; i < readyProcessList.length; i++) {
                    if (readyProcessList[i] == runProcess && readyProcessList[i] != null) {
                        runProcessIndex = i;
                        break;
                    }
                }

                //将等待运行的程序存入waitingRunningProcessList
                waitingRunningProcessList = removeArrayEmptyTextBackNewArray(readyProcessList, READY, runProcessIndex);

                //将运行结束后的程序存入endProcessList
                endProcessList = removeArrayEmptyTextBackNewArray(readyProcessList, END, -1);

                //将运行中阻塞的程序存入blockingProcessList
                blockingProcessList = removeArrayEmptyTextBackNewArray(readyProcessList, BLOCK, -1);

                //将等待的进程显示在面板
                showWaitingProcessBox();
                //将阻塞的进程显示在面板
                showBlockingProcessBox();
                //将运行结束的进程显示在面板
                showEndProcessBox();
                //显示运行的程序到面板
                if (runProcess == null) {
                    textField11.setText("-");
                    textField12.setText("-");
                    textField13.setText("-");
                    textField14.setText("-");
                    textField15.setText("-");
                } else {
                    textField11.setText(runProcess.processName);
                    textField12.setText(String.valueOf(runProcess.priority));
                    textField13.setText(String.valueOf(runProcess.CPURunningTime));
                    textField14.setText(RUNNING);
                    textField15.setText(String.valueOf(runProcess.orders));
                }

                //显示物理块到面板
                if (runProcess != null) {
                    showPhysicalBlock();
                }

                //全部运行完后的处理
                if (runProcessIndex == -1) {
                    runProcess = new PCB();
                    runProcess.processName = "NONE";
                    runProcess.priority = -1;
                    runProcess.CPURunningTime = -1;
                    runProcess.state = "FINISH_ALL";
                    runProcess.blockingTime = -1;
                    runProcess.orders = -1;
                }

                if (runProcessIndex == -1 && blockingProcessList.length == 0) {
                    System.out.println("*************");
                    System.out.println("执行完了所有程序");
                    System.out.println("*************");
                }


            }
        });

        //增加进程
        inputButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int actuallyEffectiveProcessNumber = 0;
                for (int i = 0; i < readyProcessList.length; i++) {
                    if (readyProcessList[i].processName != null) {
                        actuallyEffectiveProcessNumber++;
                    }
                }
                readyProcessList[actuallyEffectiveProcessNumber].processName = textField1.getText();
                readyProcessList[actuallyEffectiveProcessNumber].priority = Integer.valueOf(textField2.getText());
                readyProcessList[actuallyEffectiveProcessNumber].CPURunningTime = Integer.valueOf(textField3.getText());
                readyProcessList[actuallyEffectiveProcessNumber].state = textField4.getText();
                readyProcessList[actuallyEffectiveProcessNumber].blockingTime = 0;
                readyProcessList[actuallyEffectiveProcessNumber].orders = Integer.valueOf(textField5.getText());
                readyProcessList[actuallyEffectiveProcessNumber].processAddressArrayIndex = 0;
                readyProcessList[actuallyEffectiveProcessNumber].ordersLOCK = Integer.valueOf(textField5.getText());

                ordersAddressTOIndex = ordersAddressTOIndex + inputProcessList[actuallyEffectiveProcessNumber].orders;
                inputProcessList[actuallyEffectiveProcessNumber].orderSet = Arrays.copyOfRange(ordersAddress, ordersAddressFromIndex, ordersAddressTOIndex);
                ordersAddressFromIndex = ordersAddressTOIndex;

                int memoryIntArrayIndex = (int) (3 + Math.random() * (7 - 3 + 1));
                readyProcessList[actuallyEffectiveProcessNumber].physicalBlocksLength = memoryIntArrayIndex;
                readyProcessList[actuallyEffectiveProcessNumber].physicalBlockIDArray = new int[memoryIntArrayIndex];
                physicalBlockIDArrayTOIndex = physicalBlockIDArrayTOIndex + readyProcessList[actuallyEffectiveProcessNumber].physicalBlocksLength;
                readyProcessList[actuallyEffectiveProcessNumber].physicalBlockIDArray = Arrays.copyOfRange(memoryIntArray, physicalBlockIDArrayFromIndex, physicalBlockIDArrayTOIndex);
                physicalBlockIDArrayFromIndex = physicalBlockIDArrayTOIndex;

                System.out.println("============readyProcessList[actuallyEffectiveProcessNumber]===========");
                System.out.println(readyProcessList[actuallyEffectiveProcessNumber]);
                //给数组从大到小排序
                readyProcessListSort(readyProcessList);

                runProcess = null;

                for (int i = 0; i < readyProcessList.length; i++) {
                    if (readyProcessList[i].state.equals(READY)) {
                        runProcess = readyProcessList[i];
                        break;
                    }
                }
                //找到runProcess在readyProcessList中的位置
                int runProcessIndex = -1;
                for (int i = 0; i < readyProcessList.length; i++) {
                    if (readyProcessList[i] == runProcess && readyProcessList[i] != null) {
                        runProcessIndex = i;
                        break;
                    }
                }
                //将等待运行的程序存入waitingRunningProcessList
                waitingRunningProcessList = removeArrayEmptyTextBackNewArray(readyProcessList, READY, runProcessIndex);
                //将等待的进程显示在面板
                showWaitingProcessBox();
                //显示运行的程序到面板
                if (runProcess == null) {
                    textField11.setText("-");
                    textField12.setText("-");
                    textField13.setText("-");
                    textField14.setText("-");
                    textField15.setText("-");
                } else {
                    textField11.setText(runProcess.processName);
                    textField12.setText(String.valueOf(runProcess.priority));
                    textField13.setText(String.valueOf(runProcess.CPURunningTime));
                    textField14.setText(RUNNING);
                    textField15.setText(String.valueOf(runProcess.orders));
                }
            }
        });
        inputButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int actuallyEffectiveProcessNumber = 0;
                for (int i = 0; i < readyProcessList.length; i++) {
                    if (readyProcessList[i].processName != null) {
                        actuallyEffectiveProcessNumber++;
                    }
                }
                readyProcessList[actuallyEffectiveProcessNumber].processName = textField6.getText();
                readyProcessList[actuallyEffectiveProcessNumber].priority = Integer.valueOf(textField7.getText());
                readyProcessList[actuallyEffectiveProcessNumber].CPURunningTime = Integer.valueOf(textField8.getText());
                readyProcessList[actuallyEffectiveProcessNumber].state = textField9.getText();
                readyProcessList[actuallyEffectiveProcessNumber].blockingTime = 0;
                readyProcessList[actuallyEffectiveProcessNumber].orders = Integer.valueOf(textField10.getText());
                readyProcessList[actuallyEffectiveProcessNumber].processAddressArrayIndex = 0;
                readyProcessList[actuallyEffectiveProcessNumber].ordersLOCK = Integer.valueOf(textField10.getText());

                ordersAddressTOIndex = ordersAddressTOIndex + inputProcessList[actuallyEffectiveProcessNumber].orders;
                inputProcessList[actuallyEffectiveProcessNumber].orderSet = Arrays.copyOfRange(ordersAddress, ordersAddressFromIndex, ordersAddressTOIndex);
                ordersAddressFromIndex = ordersAddressTOIndex;

                int memoryIntArrayIndex = (int) (3 + Math.random() * (7 - 3 + 1));
                readyProcessList[actuallyEffectiveProcessNumber].physicalBlocksLength = memoryIntArrayIndex;
                readyProcessList[actuallyEffectiveProcessNumber].physicalBlockIDArray = new int[memoryIntArrayIndex];
                physicalBlockIDArrayTOIndex = physicalBlockIDArrayTOIndex + readyProcessList[actuallyEffectiveProcessNumber].physicalBlocksLength;
                readyProcessList[actuallyEffectiveProcessNumber].physicalBlockIDArray = Arrays.copyOfRange(memoryIntArray, physicalBlockIDArrayFromIndex, physicalBlockIDArrayTOIndex);
                physicalBlockIDArrayFromIndex = physicalBlockIDArrayTOIndex;

                System.out.println("============readyProcessList[actuallyEffectiveProcessNumber]===========");
                System.out.println(readyProcessList[actuallyEffectiveProcessNumber]);
                //给数组从大到小排序
                readyProcessListSort(readyProcessList);

                runProcess = null;

                for (int i = 0; i < readyProcessList.length; i++) {
                    if (readyProcessList[i].state.equals(READY)) {
                        runProcess = readyProcessList[i];
                        break;
                    }
                }
                //找到runProcess在readyProcessList中的位置
                int runProcessIndex = -1;
                for (int i = 0; i < readyProcessList.length; i++) {
                    if (readyProcessList[i] == runProcess && readyProcessList[i] != null) {
                        runProcessIndex = i;
                        break;
                    }
                }
                //将等待运行的程序存入waitingRunningProcessList
                waitingRunningProcessList = removeArrayEmptyTextBackNewArray(readyProcessList, READY, runProcessIndex);
                //将等待的进程显示在面板
                showWaitingProcessBox();
                //显示运行的程序到面板
                if (runProcess == null) {
                    textField11.setText("-");
                    textField12.setText("-");
                    textField13.setText("-");
                    textField14.setText("-");
                    textField15.setText("-");
                } else {
                    textField11.setText(runProcess.processName);
                    textField12.setText(String.valueOf(runProcess.priority));
                    textField13.setText(String.valueOf(runProcess.CPURunningTime));
                    textField14.setText(RUNNING);
                    textField15.setText(String.valueOf(runProcess.orders));
                }
            }
        });

    }

    //初始化
    public void initialization() {
        for (int i = 0; i < processListMAXLength; i++) {
            inputProcessList[i] = new PCB();
            System.out.println("------inputProcessList-------");
            System.out.println(inputProcessList[i]);

        }
        System.out.println("end");

        /*for (int i = 0; i < 4; i++) {
            physicalBlocks[i] = new PhysicalBlock();
        }*/

        pc = 0;
        n = 0;
        orderLength = 0;

        processAddressLength0 = 0;
        processAddressLength1 = 0;
        processAddressLength2 = 0;
        processAddressLength3 = 0;
        processAddressLength4 = 0;
    }

    //初始化各进程物理块
    public void initializationPhysicalBlock() {
        for (int i = 0; i < inputProcessList.length; i++) {
            int memoryIntArrayIndex = (int) (3 + Math.random() * (7 - 3 + 1));
            inputProcessList[i].physicalBlocksLength = memoryIntArrayIndex;
            inputProcessList[i].physicalBlockIDArray = new int[memoryIntArrayIndex];

            physicalBlockIDArrayTOIndex = physicalBlockIDArrayTOIndex + inputProcessList[i].physicalBlocksLength;
            inputProcessList[i].physicalBlockIDArray = Arrays.copyOfRange(memoryIntArray, physicalBlockIDArrayFromIndex, physicalBlockIDArrayTOIndex);
            physicalBlockIDArrayFromIndex = physicalBlockIDArrayTOIndex;
            /*System.out.println("************************");
            System.out.println(inputProcessList[i].processName);
            for (int j = 0; j < inputProcessList[i].physicalBlocksLength; j++) {

                System.out.print(inputProcessList[i].physicalBlocks[j].pageNum);
                System.out.print(inputProcessList[i].physicalBlocks[j].nextAccessInstruction);

            }
            System.out.println("+++++++++++++++++++");
            System.out.println("======memoryIntArrayIndex=======");
            System.out.println(inputProcessList[i].physicalBlocksLength);
            System.out.println("=========inputProcessList[i].physicalBlockIDArray========");
            for (int j = 0; j < inputProcessList[i].physicalBlocksLength; j++) {
                System.out.print(inputProcessList[i].physicalBlockIDArray[j]+" ");
            }
            System.out.println();*/
        }
    }

    //指令地址随机生成
    public void createRandomAddress() {
        Random r = new Random();
        for (int i = 0; i < 100; i++) {
            ordersAddress[i] = r.nextInt(300);
        }
    }

    //给数组从大到小排序
    public void readyProcessListSort(PCB[] result) {
        for (int i = 1; i < result.length; i++) {
            for (int j = i; j > 0; j--) {
                if (result[j - 1].priority < result[j].priority) {//大的放前面
                    PCB tmp = result[j - 1];
                    result[j - 1] = result[j];
                    result[j] = tmp;
                }
            }
        }
    }

    //int数组转String数组，过程中/10
    public void intArrayTOStringArray(int[] temp, String[] result) {
        for (int i = 0; i < temp.length; i++) {
            result[i] = String.valueOf(temp[i] / 10);
        }
    }

    //初始化进程对应的物理块数组
    public void InitializeProcessPageHome(String[] result) {
        for (int i = 0; i < result.length; i++) {
            result[i] = "-";
        }
    }

    //整合数组，去除空的内容
    public PCB[] removeArrayEmptyTextBackNewArray(PCB[] strArray, String judgeState, int runningProcessIndex) {

        List<PCB> strList = Arrays.asList(strArray);
        List<PCB> strListNew = new ArrayList<>();

        for (int i = 0; i < strList.size(); i++) {
            if (strList.get(i).processName == null) {
                continue;
            }
            if (runningProcessIndex == i) {
                continue;
            }
            if (strList.get(i).state == null) {
                continue;
            }

            if (strList.get(i) != null && strList.get(i).state.equals(judgeState) && runningProcessIndex != i) {
                strListNew.add(strList.get(i));
            }

        }

        PCB[] strNewArray = strListNew.toArray(new PCB[strListNew.size()]);
        return strNewArray;

    }

    //将等待的进程显示在面板
    public void showWaitingProcessBox() {

        String[] waitingRunningProcessNameArray = new String[processListMAXLength];
        String[] waitingRunningProcessPriorityArray = new String[processListMAXLength];
        String[] waitingRunningProcessStateArray = new String[processListMAXLength];
        String[] waitingRunningProcessOrdersArray = new String[processListMAXLength];

        for (int i = 0; i < waitingRunningProcessList.length; i++) {
            waitingRunningProcessNameArray[i] = waitingRunningProcessList[i].processName;
            waitingRunningProcessPriorityArray[i] = String.valueOf(waitingRunningProcessList[i].priority);
            waitingRunningProcessStateArray[i] = waitingRunningProcessList[i].state;
            waitingRunningProcessOrdersArray[i] = String.valueOf(waitingRunningProcessList[i].orders);
        }

        list8.setListData(waitingRunningProcessNameArray);
        list9.setListData(waitingRunningProcessPriorityArray);
        list10.setListData(waitingRunningProcessStateArray);
        list11.setListData(waitingRunningProcessOrdersArray);

    }

    //将阻塞结束的进程显示在面板
    public void showBlockingProcessBox() {

        String[] blockingProcessNameArray = new String[processListMAXLength];
        String[] blockingProcessBlockingTimeArray = new String[processListMAXLength];

        for (int i = 0; i < blockingProcessList.length; i++) {
            blockingProcessNameArray[i] = blockingProcessList[i].processName;
            blockingProcessBlockingTimeArray[i] = String.valueOf(blockingProcessList[i].blockingTime);
        }

        list15.setListData(blockingProcessNameArray);
        list16.setListData(blockingProcessBlockingTimeArray);

    }

    //将运行结束的进程显示在面板
    public void showEndProcessBox() {

        String[] endProcessNameArray = new String[processListMAXLength];

        for (int i = 0; i < endProcessList.length; i++) {
            endProcessNameArray[i] = endProcessList[i].processName;
        }

        list17.setListData(endProcessNameArray);

    }


    //定义生成随机数并且装入集合容器的方法
    //方法的形参列表分别为：生成随机数的个数、生成随机数的值的范围最小值为start(包含start)、值得范围最大值为end(不包含end)  可取值范围可表示为[start,end)
    public List<Integer> getRandomNumList(int nums, int start, int end) {
        //1.创建集合容器对象
        List<Integer> list = new ArrayList();
        //2.创建Random对象
        Random r = new Random();
        //循环将得到的随机数进行判断，如果随机数不存在于集合中，则将随机数放入集合中，如果存在，则将随机数丢弃不做操作，进行下一次循环，直到集合长度等于nums
        while (list.size() != nums) {
            int num = r.nextInt(end - start) + start;
            if (!list.contains(num)) {
                list.add(num);
            }
        }

        return list;
    }

    //int数组转String数组
    public void intArrayTOString(int[] temp, String[] result) {
        for (int i = 0; i < temp.length; i++) {
            result[i] = String.valueOf(temp[i]);
        }
    }


    //检测到内存/物理块中是否有该页面，-1表示没有
    public int findExist(int curPage) {
        for (int i = 0; i < runProcess.physicalBlocksLength; i++) {
            if (runProcess.physicalBlocks[i].pageNum == curPage)
                //检测到内存中有该页面，返回block中的位置
                return i;
        }
        return -1;
    }

    //找到空闲的物理块
    public int findSpace() {
        for (int i = 0; i < runProcess.physicalBlocksLength; i++) {
            if (runProcess.physicalBlocks[i].pageNum == -1)
                //找到空闲的block，返回block中的位置
                return i;
        }
        return -1;
    }

    //找到应予置换页面，返回BLOCK中位置，越少使用的nextAccessInstruction越大
    public int findReplace() {
        int positionIndex = 0;
        for (int i = 0; i < runProcess.physicalBlocksLength; i++) {
            if (runProcess.physicalBlocks[i].nextAccessInstruction > runProcess.physicalBlocks[positionIndex].nextAccessInstruction)
                //找到应予置换页面，返回BLOCK中位置
                positionIndex = i;
        }
        return positionIndex;
    }

    //打印四个块中页号
    public void display() {
        System.out.println("*****");
        for (int i = 0; i < runProcess.physicalBlocksLength; i++) {
            if (runProcess.physicalBlocks[i].pageNum != -1) {
                System.out.print(runProcess.physicalBlocks[i].pageNum + " ");
            }
        }
        System.out.println("*****");
    }

    //LRU算法
    public void LRU(int temp) {
        int exist, space, position;
        int curPage;

        pc = temp;
        curPage = pc / 10;
        //检测到内存/物理块中是否有该页面，-1表示没有，其他值表示该页存在且位置在这
        exist = findExist(curPage);
        //没有该页，进行存该页操作
        if (exist == -1) {
            n++;
            //找到空闲的物理块，没有空闲是-1，有空闲则是该物理块位置
            space = findSpace();
            //space != -1 即有空闲，将该页存入
            if (space != -1) {
                runProcess.physicalBlocks[space].pageNum = curPage;
                //打印四个块中页号
                display();

            } else {
                //找到应予置换页面，返回BLOCK中位置
                position = findReplace();
                runProcess.physicalBlocks[position].pageNum = curPage;
                //打印四个块中页号
                display();
            }
        } else {
            //最近使用过，赋值为0
            runProcess.physicalBlocks[exist].nextAccessInstruction = 0;
        }
        //每块中存在的页面未使用时长加1，下次寻找该值最大的块置换出去
        //规定越少使用的nextAccessInstruction越大，越多使用的nextAccessInstruction越小
        for (int j = 0; j < runProcess.physicalBlocksLength; j++) {
            if (j == exist) continue;
            runProcess.physicalBlocks[j].nextAccessInstruction++;
        }


        System.out.println("缺页次数: " + n);
        System.out.println("缺页率: " + ((n * 0.1) / (orderLength * 0.1)));
    }

    //在物理块中显示
    public void showPhysicalBlock() {
        String[] physicalBlock = new String[runProcess.physicalBlocksLength];
        for (int i = 0; i < runProcess.physicalBlocksLength; i++) {
            if (runProcess.physicalBlocks[i].pageNum != -1) {
                //printf(" %02d",block[i].pagenum);
                physicalBlock[i] = String.valueOf(runProcess.physicalBlocks[i].pageNum);
            } else {
                physicalBlock[i] = "#";
            }
        }
        list12.setListData(physicalBlock);
    }

    //指令的具体内容
    public String ordersExecution() {
        String[] ordersType = {"读内存", "写内存", "输入", "输出"};
        Random random = new Random();
        int ordersTypeIndex;
        ordersTypeIndex = random.nextInt(4);
        textField16.setText(ordersType[ordersTypeIndex]);
        return ordersType[ordersTypeIndex];
    }

    //改变进程对应的物理块
    public void changeProcessPageHome(int[] temp, String[] resultProcessPageHome) {
        for (int i = 0; i < temp.length; i++) {
            //检测到物理块中是否有该页面,参数是页号
            int exist = findExist(temp[i] / 10);
            if (exist != -1) {
                resultProcessPageHome[i] = runProcess.physicalBlockIDArray[exist] + "";
            } else {
                resultProcessPageHome[i] = "*";

            }
        }
    }

    //设置空闲内存块
    public int[] selectMemoryFreeArea() {

        Random random = new Random();
        int showStartPosition;
        showStartPosition = random.nextInt(15);
        int showWordsIndex = showStartPosition;
        int realShowStartPosition = showStartPosition;
        int length = 50 - showStartPosition;
        int[] showTemp = new int[length];
        String[] freeArray = new String[length];
        for (int i = 0; i < length; i++) {
            showTemp[i] = showStartPosition++;
            freeArray[i] = "0";
        }
        int[] temp = new int[200];
        for (int i = 0; i < 200; i++) {
            temp[i] = realShowStartPosition++;
        }
        String[] tempString = new String[length];
        intArrayTOString(showTemp, tempString);

        //只展示空闲的前60 - startPosition条
        list6.setListData(tempString);
        list7.setListData(freeArray);

        String showWords = 0 + " - " + (showWordsIndex-1);
        textField17.setText(showWords);
        textField18.setText("1");

        return temp;
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame("CurriculumDesignViews");
        frame.setContentPane(new CurriculumDesignViews().mainJPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(3000, 3000);
    }
}
